<?php

require_once APPLICATION_PATH . '/models/AclModel.php';
require_once 'Okute/Controller/Action/Helper/Redirector.php';
require_once 'Okute/Controller/Plugin/Mobile.php';
require_once 'Okute/Controller/Plugin/Security.php';

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    protected function _initPlugin() {
        $options = new Zend_Config($this->getOptions());
        Zend_Registry::set('options', $options);

        $this->bootstrap('FrontController');
        $front = $this->getResource('FrontController');

        $front->registerPlugin(new Okute_Controller_Plugin_Mobile());
        $front->registerPlugin(new Okute_Controller_Plugin_Security());
    }

    protected function _initView() {
        require_once('Smarty/Smarty.class.php');
        require_once('Okute/View/Smarty.php');

        $options = new Zend_Config($this->getOptions());
        $config = $options->smarty;

        $view = new Okute_View_Smarty(
            $config->template_dir->default,
            array('compile_dir' => $config->compile_dir,
                  'config_dir' => $config->config_dir,
                  'cache_dir' => $config->cache_dir),
            array('addPluginsDir' => array($config->plugins_dir)));

        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
            'ViewRenderer'
        );

        $viewRenderer->setView($view)
            ->setViewBasePathSpec($view->getEngine()->template_dir)
            ->setViewScriptPathSpec(':controller/:action.:suffix')
            ->setViewScriptPathNoControllerSpec(':action.:suffix')
            ->setViewSuffix($config->template_extension);

        $view->charset = $config->output_charset->default;
        Zend_Registry::set('charset', $config->output_charset);
        header("Content-type: text/html; charset=".$config->output_charset->default);
        return $view;
    }

    protected function _initDb() {
        $resource = $this->getPluginResource('db');
        $db = Zend_Db::factory($resource->getAdapter(),$resource->getParams());
        Zend_Registry::set('database', $db);
        Zend_Db_Table::setDefaultAdapter($db);
    }

    protected function _initOkute() {
        $options = new Zend_Config($this->getOptions());
        $config = $options->okute;
        Zend_Registry::set('config', $config);

        Zend_Controller_Action_HelperBroker::addPath(
            'Okute/Controller/Action/Helper', 'Okute_Controller_Action_Helper'
        );
        $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
        Zend_Registry::set('redirector', $redirector);
    }

    protected function _initAcl() {
        $acl = new AclModel();
        Zend_Registry::set('acl', $acl);
    }

}